//
//  TrainDetailTableViewController.swift
//  YueJian
//
//  Created by mac on 2017/10/19.
//  Copyright © 2017年 MapleStory. All rights reserved.
//

import UIKit
import Alamofire

class TrainDetailTableViewController: UITableViewController {
    var id = ""
    var detail = [String:AnyObject]()

    override func viewDidLoad() {
        super.viewDidLoad()
        let id = UserDefaults.standard.string(forKey:"sessionId") ?? ""
        let paramas = ["SessionID":id, "page":1, "mod":"train"] as [String : Any]
        print(paramas)
        Alamofire.request(URL(string: Search_Train)!, method: .post, parameters: paramas, encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            
            print(response.result.value)
            let array = response.result.value as? [AnyObject]
            for i in 0..<array!.count {
                let dic = array![i] as! [String:AnyObject]
                var sid = dic["id"] as! String
                if sid == self.id {
                    self.detail = dic
                }
            }
            self.tableView.reloadData()
            
//            let str = self.detail["thumb"] as? String
//            let imageStr = "http://172.24.10.175/workout/Uploads/" + (str ?? "")
//            let images = try! UIImage(data: Data(contentsOf: URL(string: imageStr)!))
//
//            let imageview = UIImageView()
//            imageview.frame = CGRect(x: 0, y: 0, width: Screen_Width, height: 180)
//            imageview.image = images
//
//            self.tableView.tableHeaderView = imageview
        }
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellFoot")
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cellHead")
        tableView.separatorStyle = .none
//        tableView.rowHeight = UITableViewAutomaticDimension
//        tableView.estimatedRowHeight = 200
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        return 6
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 1
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        if indexPath.section == 5 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellFoot", for: indexPath)
            cell.selectionStyle = .none
            let bgview = UIView()
            bgview.backgroundColor = UIColor.white
            
            for i in 0...2 {
                let button = UIButton(frame: CGRect(x: (Screen_Width-2)/3*CGFloat(i), y: 0, width: (Screen_Width-2)/3, height: 50))
                bgview.addSubview(button)
                button.backgroundColor = #colorLiteral(red: 0.09019608051, green: 0, blue: 0.3019607961, alpha: 1)
                switch i {
                case 0:
                    button.setTitle("¥160", for: .normal)
                    button.setTitleColor(UIColor.white, for: .normal)
                case 1:
                    button.setTitle("立即咨询", for: .normal)
                    button.backgroundColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
                case 2:
                    button.setTitle("立即报名", for: .normal)
                    button.backgroundColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
            
                default:
                    break
                }
            }
            
            cell.backgroundView = bgview
            return cell
        } else if indexPath.section == 0{
            let cell = tableView.dequeueReusableCell(withIdentifier: "cellHead", for: indexPath)
            let str = detail["thumb"] as? String
            let imageStr = "http://172.24.10.175/workout/Uploads/" + (str ?? "")
            let images = try! UIImage(data: Data(contentsOf: URL(string: imageStr)!))
            let imageview = UIImageView()
            imageview.frame = CGRect(x: 0, y: 0, width: Screen_Width, height: 180)
            imageview.image = images
            cell.backgroundView = imageview
          cell.selectionStyle = .none
            return cell
        }else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
            switch indexPath.section {
            case 1:
                cell.textLabel?.text = self.detail["intro"] as? String
            case 2:
                cell.textLabel?.text = self.detail["notice"] as? String
            case 3:
                cell.textLabel?.text = self.detail["update_time"] as? String
            case 4:
                cell.textLabel?.text = "大连"
            default:break
            }
            cell.textLabel?.numberOfLines = 0
            cell.textLabel?.font = UIFont.systemFont(ofSize: 13)
            cell.selectionStyle = .none
            return cell
        }
    }
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        let array = ["课程简介","上课须知","授课时间","授课地点"]
        if section == 0 || section == 5 {
            return ""
        } else {
            return array[section-1]
        }
    }
    
    

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
      
            if indexPath.section == 0 {
                return 180
            } else {
                return UITableViewAutomaticDimension
            }
        
    }


//
//    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
//        let str = detail["thumb"] as! String
//        let imageStr = "http://172.24.10.175/workout/Uploads/"+str
//        let images = try! UIImage(data: Data(contentsOf: URL(string: imageStr)!))
//        let head = UIImageView(image: images)
//        return head
//    }
//    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
//
//        let bgview = UIView()
//        bgview.backgroundColor = UIColor.white
//        for i in 0...2 {
//            let button = UIButton(frame: CGRect(x: (Screen_Width-2)/3*CGFloat(i), y: 0, width: (Screen_Width-2)/3, height: 50))
//            bgview.addSubview(button)
//            button.backgroundColor = #colorLiteral(red: 0.09019608051, green: 0, blue: 0.3019607961, alpha: 1)
//            switch i {
//            case 0:
//                button.setTitle("¥160", for: .normal)
//                button.setTitleColor(UIColor.white, for: .normal)
//            case 1:
//                button.setTitle("立即咨询", for: .normal)
//                button.backgroundColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
//            case 2:
//                button.setTitle("立即报名", for: .normal)
//                button.backgroundColor = #colorLiteral(red: 0.2745098174, green: 0.4862745106, blue: 0.1411764771, alpha: 1)
//                button.addTarget(self, action: #selector(goApply), for: .touchUpInside)
//            default:
//                break
//            }
//        }
//
//        return bgview
//
//    }

  
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section == 5 {
            let vc = ApplyViewController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }

    /*
    // Override to support conditional editing of the table view.
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            // Delete the row from the data source
            tableView.deleteRows(at: [indexPath], with: .fade)
        } else if editingStyle == .insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
